package cn.bytepulse.web.admin.controller;

import cn.bytepulse.annotation.Log;
import cn.bytepulse.pojo.AjaxResult;
import cn.bytepulse.web.admin.entity.SysRole;
import cn.bytepulse.web.admin.service.SystemRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author jiejiebiezheyang
 * @since 2024-07-09 22:00
 */
@Tag(name = "管理-系统管理-角色管理")
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class SystemRoleController {

    private final SystemRoleService systemRoleService;

    @GetMapping("/list")
    @Operation(summary = "角色列表")
    @Log(value = "管理-系统管理-角色管理-角色列表", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:query')")
    public AjaxResult list() {
        return systemRoleService.list();
    }

    @PostMapping("/add")
    @Operation(summary = "添加角色")
    @Log(value = "管理-系统管理-角色管理-添加角色", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:add')")
    public AjaxResult add(@RequestBody @Validated({SysRole.CreateRole.class}) SysRole sysRole) {
        return systemRoleService.add(sysRole);
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色")
    @Log(value = "管理-系统管理-角色管理-更新角色", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:update')")
    public AjaxResult update(@RequestBody @Validated({SysRole.UpdateRole.class}) SysRole sysRole) {
        return systemRoleService.update(sysRole);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @Log(value = "管理-系统管理-角色管理-删除角色", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:delete')")
    public AjaxResult delete(@RequestParam Integer id) {
        return systemRoleService.delete(id);
    }

    @GetMapping("/getRelation")
    @Operation(summary = "获取关联菜单")
    @Log(value = "管理-系统管理-角色管理-获取关联菜单", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:relation:query')")
    public AjaxResult getRelation(@RequestParam Integer id) {
        return systemRoleService.getRelation(id);
    }

    @GetMapping("/getMenuConcise")
    @Operation(summary = "获取菜单简要")
    @Log(value = "管理-系统管理-角色管理-获取菜单简要", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:relation:query')")
    public AjaxResult getMenuConcise() {
        return systemRoleService.getMenuConcise();
    }

    @PostMapping("/updateRelation")
    @Operation(summary = "更新角色菜单关联")
    @Log(value = "管理-系统管理-角色管理-更新角色菜单关联", persist = true)
    @PreAuthorize("hasAnyAuthority('system:role:relation:update')")
    public AjaxResult updateRelation(@RequestParam Integer roleId, Integer[] menuIds) {
        return systemRoleService.updateRelation(roleId, menuIds);
    }
}
